package com.tensquare.web.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author:zimmor
 * @Date: 2021/3/16 19:04
 * @Description
 * @version: 1.0.0
 */
@Component
public class WebFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //因为header经过一次请求就没有，所有我们要转发请求中的header信息
        System.out.println("经过了前端网关");
        //获取request上下文
        RequestContext currentContext = RequestContext.getCurrentContext();
        //获取request
        HttpServletRequest request = currentContext.getRequest();
        //获取头信息
        String authorization = request.getHeader("Authorization");
        //转发头信息
        if(authorization!=null && !"".equals(authorization)) {
            currentContext.addZuulRequestHeader("Authorization", authorization);
        }
        return null;
    }
}
